﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class Layout : ITemplate, IXmlBuildable, IChildDeletable
    {
        public string LayoutTitle { get; set; }
        public string Title { get; set; }
        public List<OverflowSection> OverflowSections { get; set; }
        public List<Section> Sections { get; set; }

        public Layout()
        {
            this.OverflowSections = new List<OverflowSection>();
            this.Sections = new List<Section>();
        }

        public Layout(XmlNode node): this()
        {
            try
            {
                this.LayoutTitle = XmlHelper.GetAttribute(node, "LayoutTitle");
                this.Title = XmlHelper.GetAttribute(node, "Title");

                foreach (XmlNode overflowSectionNode in node.SelectNodes("OverflowSection"))
                {
                    OverflowSection oSection = new OverflowSection(node.SelectSingleNode("OverflowSection"));
                    this.OverflowSections.Add(oSection);
                }

                foreach (XmlNode overflowSectionNode in node.SelectNodes("Section"))
                {
                    Section section = new Section(node.SelectSingleNode("Section"));
                    this.Sections.Add(section);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode groupTemplateNode)
        {
            try
            {
                XmlNode layoutNode = XmlHelper.AddNode(groupTemplateNode, "Layout");
                XmlHelper.AddAttribute(layoutNode, "LayoutTitle", this.LayoutTitle);
                XmlHelper.AddAttribute(layoutNode, "Title", this.Title);

                foreach (OverflowSection oSection in this.OverflowSections)
                {
                    oSection.AddNode(layoutNode);
                }

                foreach (Section section in this.Sections)
                {
                    section.AddNode(layoutNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Layout");
                node.ContextMenuStrip = BuildContextMenu(node);
                node.Tag = this;

                foreach (OverflowSection oSection in this.OverflowSections)
                {
                    oSection.BuildTreeViewElement(node);
                }

                foreach (Section section in this.Sections)
                {
                    section.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Overflow Section");
            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("Add Section");

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;
            cms.Items.Add(tsmi);
            cms.Items.Add(tsmi2);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                if (e.ClickedItem.Text == "Add Overflow Section")
                {
                    OverflowSection oSection = new OverflowSection();
                    oSection.BuildTreeViewElement(selectedNode);
                    this.OverflowSections.Add(oSection);
                }
                else
                {
                    Section section = new Section();
                    section.BuildTreeViewElement(selectedNode);
                    this.Sections.Add(section);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is OverflowSection)
                {
                    this.OverflowSections.Remove((OverflowSection)o);
                }
                else if (o is Section)
                {
                    this.Sections.Remove((Section)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
